//
//  JSOrderDetailController.m
//  jshop
//
//  Created by 张艳江 on 2019/5/21.
//  Copyright © 2019 张艳江. All rights reserved.
//

#import "SCOrderDetailController.h"
#import "SCOrderDetailHeadView.h"
#import "SCOrderDetailFootView.h"
#import "SCOrderListCell.h"
#import "SCRefundController.h"
#import "SCOrderEvaluaController.h"

@interface SCOrderDetailController ()<UITableViewDelegate,UITableViewDataSource,SCOrderListCellDelegate>

@property (weak, nonatomic) IBOutlet UIButton *rightBtn;
@property (weak, nonatomic) IBOutlet UIButton *leftBtn;

@property (strong, nonatomic) UITableView           *tableView;
@property (strong, nonatomic) SCOrderDetailHeadView *headView;
@property (strong, nonatomic) SCOrderDetailFootView *footView;
@property (strong, nonatomic) SCModel               *detailModel;

@end

@implementation SCOrderDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"订单详情";
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem barButtonItemWithImage:[UIImage imageNamed:@"back"] target:self action:@selector(clickBackItem)];
    
    [self setUpTableView];
    
    //请求数据
    [SVProgressHUD showWithStatus:@"加载中..."];
    [self requestDetailData];
}
- (void)clickBackItem{
    if (self.isPayPush == YES) {
        [self.navigationController popToRootViewControllerAnimated:YES];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}
#pragma mark - 表格
- (void)setUpTableView{
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH - kTopHeight) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = BGColor;
    self.tableView.rowHeight = 102;
    self.tableView.separatorColor = [UIColor clearColor];
    // 注册
    [self.tableView registerNib:[UINib nibWithNibName:@"SCOrderListCell" bundle:nil] forCellReuseIdentifier:@"SCOrderListCellID"];
    [self.view addSubview:self.tableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.detailModel.list.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SCOrderListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SCOrderListCellID"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.detailModel = self.detailModel.list[indexPath.row];
    cell.delegate = self;
    return cell;
}
- (void)clickCellButton:(SCModel *)model tag:(NSInteger)tag{
    if (tag == 0) {//退款退货
        SCRefundController *vc = [[SCRefundController alloc]init];
        vc.goodsModel = model;
        [self.navigationController pushViewController:vc animated:YES];
    }else{//确认收货
        [MyTool alertTitle:@"您确定要确认收货?" mesasge:nil preferredStyle:UIAlertControllerStyleAlert confirmHandler:^(UIAlertAction * _Nonnull action) {
            
            NSString *order_id = [NSString stringWithFormat:@"[%@]",model.order_id];
            NSDictionary *dict = @{@"type":model.type,
                                   @"token":Token,
                                   @"order_id":order_id,
                                   @"status":@"4"};
            //NSLog(@"====%@",dict);
            NSString *url = [MyTool splicingWithUrl:@"admin.php?g=System&c=GiftAppOrder&a=App_status"];
            [SVProgressHUD showWithStatus:@"确认中..."];
            [[NetworkRequestTool sharedNetworkTool] POST:url parameters:dict finished:^(id result, NSString *error) {
                [SVProgressHUD showSuccessWithStatus:@"确认成功"];
                model.status = @"4";
                [self.tableView reloadData];
            }];
        } cancleHandler:nil viewController:self];
    }
}
#pragma mark - 请求数据
- (void)requestDetailData{
    
    NSDictionary *dict = @{@"token":Token,
                           @"orderid":self.orderid,
                           @"type":self.type};
    //NSLog(@"===%@",dict);
    NSString *url = [MyTool splicingWithUrl:@"admin.php?g=System&c=GiftAppOrder&a=App_orderDetails"];
    [SVProgressHUD showWithStatus:@"加载中..."];
    [[NetworkRequestTool sharedNetworkTool] POST:url parameters:dict finished:^(id result, NSString *error) {
        [SVProgressHUD dismiss];
        NSLog(@"-----%@",result);
        
        self.detailModel = [SCModel mj_objectWithKeyValues:result[@"data"]];
        self.detailModel.list = [SCModel mj_objectArrayWithKeyValuesArray:self.detailModel.list];
        [self.tableView reloadData];
        
        self.headView = [SCOrderDetailHeadView addOrderDetailHeadView];
        self.headView.frame = CGRectMake(0, 0, kScreenW, 185);
        self.headView.model = [SCModel mj_objectWithKeyValues:result[@"data"][@"address"]];
        self.tableView.tableHeaderView = self.headView;
        
        self.footView = [SCOrderDetailFootView addOrderDetailFootView];
        self.footView.frame = CGRectMake(0, 0, kScreenW, 230);
        self.footView.model = self.detailModel;
        self.tableView.tableFooterView = self.footView;
    }];
}
#pragma mark - 点击底部按钮
- (IBAction)clickBtns:(UIButton *)sender {

    
}

@end
